import { defineStore } from 'pinia'
import { getMemberList, getMemberDetail, createMember, updateMember, deleteMember, getMemberLevelList, getMemberLevelDetail, createMemberLevel, updateMemberLevel, deleteMemberLevel } from '@/api/member'

export const useMemberStore = defineStore('member', {
  state: () => ({
    memberList: [],
    memberDetail: {},
    memberLevelList: [],
    memberLevelDetail: {},
    loading: false,
    total: 0
  }),

  actions: {
    // 获取会员列表
    async fetchMemberList(params) {
      this.loading = true
      try {
        const response = await getMemberList(params)
        this.memberList = response.data
        this.total = response.total
        return response
      } catch (error) {
        console.error('获取会员列表失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 获取会员详情
    async fetchMemberDetail(id) {
      this.loading = true
      try {
        const response = await getMemberDetail(id)
        this.memberDetail = response.data
        return response
      } catch (error) {
        console.error('获取会员详情失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 创建会员
    async createMember(data) {
      try {
        const response = await createMember(data)
        // 如果当前在会员列表页，重新获取列表
        if (this.memberList.length > 0) {
          await this.fetchMemberList({})
        }
        return response
      } catch (error) {
        console.error('创建会员失败:', error)
        throw error
      }
    },

    // 更新会员
    async updateMember(id, data) {
      try {
        const response = await updateMember(id, data)
        // 更新会员列表中的数据
        const index = this.memberList.findIndex(member => member.id === id)
        if (index !== -1) {
          this.memberList[index] = { ...this.memberList[index], ...data }
        }
        // 如果是当前详情页的会员，也更新详情数据
        if (this.memberDetail.id === id) {
          this.memberDetail = { ...this.memberDetail, ...data }
        }
        return response
      } catch (error) {
        console.error('更新会员失败:', error)
        throw error
      }
    },

    // 删除会员
    async deleteMember(id) {
      try {
        const response = await deleteMember(id)
        // 从会员列表中移除
        this.memberList = this.memberList.filter(member => member.id !== id)
        this.total--
        return response
      } catch (error) {
        console.error('删除会员失败:', error)
        throw error
      }
    },

    // 获取会员等级列表
    async fetchMemberLevelList() {
      this.loading = true
      try {
        const response = await getMemberLevelList()
        this.memberLevelList = response.data
        return response
      } catch (error) {
        console.error('获取会员等级列表失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 获取会员等级详情
    async fetchMemberLevelDetail(id) {
      this.loading = true
      try {
        const response = await getMemberLevelDetail(id)
        this.memberLevelDetail = response.data
        return response
      } catch (error) {
        console.error('获取会员等级详情失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 创建会员等级
    async createMemberLevel(data) {
      try {
        const response = await createMemberLevel(data)
        // 重新获取会员等级列表
        await this.fetchMemberLevelList()
        return response
      } catch (error) {
        console.error('创建会员等级失败:', error)
        throw error
      }
    },

    // 更新会员等级
    async updateMemberLevel(id, data) {
      try {
        const response = await updateMemberLevel(id, data)
        // 更新会员等级列表中的数据
        const index = this.memberLevelList.findIndex(level => level.id === id)
        if (index !== -1) {
          this.memberLevelList[index] = { ...this.memberLevelList[index], ...data }
        }
        // 如果是当前详情页的会员等级，也更新详情数据
        if (this.memberLevelDetail.id === id) {
          this.memberLevelDetail = { ...this.memberLevelDetail, ...data }
        }
        return response
      } catch (error) {
        console.error('更新会员等级失败:', error)
        throw error
      }
    },

    // 删除会员等级
    async deleteMemberLevel(id) {
      try {
        const response = await deleteMemberLevel(id)
        // 从会员等级列表中移除
        this.memberLevelList = this.memberLevelList.filter(level => level.id !== id)
        return response
      } catch (error) {
        console.error('删除会员等级失败:', error)
        throw error
      }
    }
  }
})